<?php

namespace app\admin\logic;

use app\common\model\mysql\ProjectVideoModel;
use app\common\logic\BaseLogic;
use app\common\service\UploadService;

class ProjectVideoLogic extends BaseLogic
{
    public function index($request)
    {
        $search = $request->param();

        $where = [['project_id', '=', $search['project_id']]];
        if (!empty($search['name'])) {
            $where[] = ['name', 'like', "%{$search['name']}%"];
        }

        $list = ProjectVideoModel::getList($where, '*', [], ['sort' => 'asc']);

        return view('ProjectVideo/video', compact('list', 'search'));
    }

    public function create($request)
    {
        $param = $request->param();

        return view('ProjectVideo/video_add', compact('param'));
    }

    public function save($request)
    {
        $param = $request->param();


        $inputData = [
            'project_id' => $param['project_id'],
            'name' => $param['name'],
            'sort' => $param['sort'],
            'status' => $param['status'],
        ];


        // 视频上传
        if (!empty($param['video'])) {
            $inputData['video'] = $param['video'];
        }

        try {
            ProjectVideoModel::addData($inputData);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功');
    }

    public function edit($request, $id)
    {
        $info = ProjectVideoModel::getOne(['id' => $id]);
        return view('ProjectVideo/video_edit', compact('info'));
    }

    public function update($request, $id)
    {
        $param = $request->param();
        $info = ProjectVideoModel::getOne(['id' => $id]);
        if (!$info) return $this->responseError('视频不存在');

        $inputData = [
            'name' => $param['name'],
            'sort' => $param['sort'],
            'status' => $param['status'],
        ];
        // 视频上传
        if (!empty($param['video'])) {
            $inputData['video'] = $param['video'];
        }
        try {
            ProjectVideoModel::editData($inputData, ['id' => $id]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }

        return $this->responseSuccess('保存成功');
    }


    public function renew($request, $id)
    {
        $param = $request->param();
        $inputData = [];
        isset($param['status']) ? $inputData['status'] = $param['status'] : false;
        isset($param['sort']) ? $inputData['sort'] = $param['sort'] : false;
        try {
            ProjectVideoModel::editData($inputData, ['id' => $id]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功');
    }
}